// can be reused by many routes
module.exports = (validations) => {
  return async (req, res, next) => {
    // sequential processing, stops running validations chain if one fails.
    for (const validation of validations) {
      const result = await validation.run(req);
      if (!result.isEmpty()) {
        var errors = result.array({ onlyFirstError: true });
        // return res.status(400).json({ errors: result.array() });
        return res.backFail(errors, errors[0].msg);
      }
    }
    next();
  };
};
